const app = getApp();
const regeneratorRuntime = require('../../utils/runtime.js')
var request = require("../../utils/request.js");
Page({
  data: {
    imgList: [],
    index: '',
    tasks: {},
    picker: [],
    pictureUrls: [],
    taskType: '',
    money: '',
    taskName: '',
    taskDescription: '',
  },
  //任务类型
  PickerChange(e) {
    var that = this;
    var index = parseInt(e.detail.value);
    this.setData({
      taskType: that.data.picker[index].typeId,
      index: e.detail.value
    })
  },
  //选择图片
  async ChooseImage() {
    wx.chooseImage({
      count: 4, //默认9
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'], //从相册或拍照选择
      success: (res) => {
        if (this.data.imgList.length != 0) {
          this.setData({
            imgList: this.data.imgList.concat(res.tempFilePaths)
          })
        } else {
          this.setData({
            imgList: res.tempFilePaths,
          })
        }
      }
    });

  },
  //回显图片
  ViewImage(e) {
    wx.previewImage({
      urls: this.data.imgList,
      current: e.currentTarget.dataset.url
    });
  },
  DelImg(e) {
    wx.showModal({
      content: '确定要删除这张图片吗？',
      cancelText: '取消',
      confirmText: '确定',
      success: res => {
        if (res.confirm) {
          this.data.imgList.splice(e.currentTarget.dataset.index, 1);
          this.setData({
            imgList: this.data.imgList
          })
        }
      }
    })
  },
  // 页面创建时加载任务类型集合
  onLoad: function () {
    this.reLoadData()
  },
  onReady: function () {
    this.reLoadData()
  },
  reLoadData: function () {
    var that = this;
    request.post('/public/taskTypeList', this.data.tasks, function (res) {
      if (res.code == 200) {
        that.setData({
          picker: res.data
        })
      } else {
        wx.showToast({
          title: '任务类型加载失败',
          icon: 'none',
          duration: 1000
        });
      }
    })
  },

  //图片上传阶段
  upload: function () {
    var that = this
    let imgArr = []
    let j = 1;
    if (that.data.imgList.length > 0) {
      for (let i = 0; i < that.data.imgList.length; i++) {
        //  执行图片上传
        request.upload('/public/uploadFile', that.data.imgList[i], function (res) {
          if (res.code == 200) {
            imgArr.push(res.data);
            that.setData({
              pictureUrls: imgArr
            });
            if (j == that.data.imgList.length) {
              that.publicTask();
            }
            j++;
          }
        })
      }
    } else {
      //发布任务
      that.publicTask();
    }
  },

  // 发布任务阶段
  publicTask: function () {
    var that = this
    request.post('/public/taskPublic', that.data, function (res) {
      if (res.code == 200) {
        wx.showToast({
          title: '任务发布成功！等待系统审核！',
          icon: 'none',
          duration: 1000
        });
        //页面跳转
        wx.reLaunch({
          url: '/pages/index/index'
        })
      } else {
        wx.showToast({
          title: '任务类型加载失败',
          icon: 'none',
          duration: 1000
        });
      }
    })
  }

})